package com.cskaoyan.service;

import com.cskaoyan.bean.MarketOrder;
import com.cskaoyan.bean.MarketOrderExample;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.StringUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author Ramsey
 * @date 2023年05月29日 17:14
 */
public class AdminOrderServiceimpl implements AdminOrderService {
    @Override
    public CommonData<MarketOrder> list(Integer userId, String orderSn, Date start, Date end, List<Short> orderStatusArray, Integer page, Integer limit, String sort, String order) {
        MarketOrderExample example = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = example.createCriteria();

        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }
        if (!StringUtil.isEmpty(orderSn)) {
            criteria.andOrderSnEqualTo(orderSn);
        }
        if (start != null) {
            criteria.andAddTimeGreaterThanOrEqualTo(start);
        }
        if (end != null) {
            criteria.andAddTimeLessThanOrEqualTo(end);
        }
        if (orderStatusArray != null && orderStatusArray.size() != 0) {
            criteria.andOrderStatusIn(orderStatusArray);
        }
        criteria.andDeletedEqualTo(false);

        if (!StringUtil.isEmpty(sort) && !StringUtil.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketOrderMapper orderMapper = sqlSession.getMapper(MarketOrderMapper.class);
        PageHelper.startPage(page, limit);
        List<MarketOrder> marketOrders = orderMapper.selectByExample(example);
        return CommonData.data(new PageInfo(marketOrders));
    }

    @Override
    public MarketOrder selectById(Integer id) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        MarketOrderMapper orderMapper = sqlSession.getMapper(MarketOrderMapper.class);
        MarketOrder marketOrder = orderMapper.selectByPrimaryKey(id);
        return marketOrder;
    }

    @Override
    public void changeStatus(Integer id) {
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketOrderMapper mapper = sqlSession.getMapper(MarketOrderMapper.class);
        MarketOrder marketOrder = mapper.selectByPrimaryKey(id);
        BigDecimal getActualPrice = marketOrder.getActualPrice();//设置用户退款金额
        mapper.updateStatueAndRefundPrice(id, getActualPrice);
    }

    @Override
    public void changeStatusByShip(Integer id, String shipChannel, String shipSn) {
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketOrderMapper mapper = sqlSession.getMapper(MarketOrderMapper.class);
        mapper.updateStatueByShip(id, shipChannel, shipSn);
    }

    @Override
    public void changeStatusByDelete(Integer orderId) {
        SqlSession sqlSession = TransactionUtil.getSqlSession();
        MarketOrderMapper mapper = sqlSession.getMapper(MarketOrderMapper.class);
        mapper.updateByDelete(orderId);
    }

}
